/*
mode by xiaoxiaoyudu
github :https://github.com/xiaoxiaoyudu
qq :1907383069
blbl:两位真实好友
*/
#include "page.h"
#include "chinese.h"
#include "weather.h"
#include "menu2.h"
extern TFT_t DEV;
static const char *TAG = "PAGE";
// extern static EventGroupHandle_t s_wifi_event_group;//wifi事件�??
//static EventGroupHandle_t mqtt_event_group;//mqtt事件�??
extern FontxFile fx16M[2];
extern FontxFile fx24M[2];
extern FontxFile fx32M[2];
extern const int CONNECTED_BIT ;
extern const int ESPTOUCH_DONE_BIT ;
extern const int WIFI_SMART;
extern BILIBILI wp_fans;//粉丝结构体
extern struct DATA_WEATHER weather_data[3];
xQueueHandle page_evt_queue = NULL;
extern char ip_adder[20];
INROOM wp;
void draw_weather(uint8_t i,int x,int y)
{
    char file[30];
    sprintf(file,"/spiffs/%s",weather[i].filename);
	PNGTest(&DEV, file, 48, 48,x,y);
}
void show_weather(uint16_t * data)
{
	uint8_t weathercode;
    //ESP_LOGI(HTTP_TAG,"%s 222",__func__);
	lcd_draw_utf8_ram(0,2,"今天",80,WHITE,data,240,102);//天气温度
	lcd_draw_utf8_ram(80,2,"明天",80,WHITE,data,240,102);//天气温度
	lcd_draw_utf8_ram(160,2,"后天",80,WHITE,data,240,102);//天气温度

    for(uint8_t i=0;i<3;i++)
    {
        weathercode=weather_data[i].code_day;
        draw_weather_Ram(weathercode,16+80*i,22,(uint8_t *)data,240);
		lcd_draw_utf8_ram(80*i,80,weather[weathercode].weathername,80,WHITE,data,240,102);//天气温度
	   // Show_Str_Mid(80*i,103,weather[weathercode].weathername,16,80, WHITE);
    }
}
void showlogo()//上电动画
{
    char file[30];
		for(uint8_t i=0;i<30;i++)
	{

		strcpy(file, "/spiffs/2.png");
		PNGTest(&DEV, file, 80, 48,10+i,30);
	//33娘
		strcpy(file, "/spiffs/3.png");
		PNGTest(&DEV, file, 80, 48,150-i,30);

	}

	Show_Str_Mid(0,80,(char *)happy,16,240, WHITE);
    WAIT(200);
	lcdFillScreen(&DEV, BLACK);//清屏


}
void page_wifi_connet()
{
	lcdFillScreen(&DEV, BLACK);//清屏
	char file[32];
	strcpy(file, "/spiffs/airkiss.jpg");
	JPEGshow(&DEV, file, 120, 120,5,5);
	Show_Str_Mid(135,50,(char *)Swx,16,64, WHITE);
}
void wifi_rate()
{
		/****开�??联网****/

	Show_Str_Mid(0,70,(char *)Sget,16,240, WHITE);
	wifi_init();

}
void fans_rate()
{
	Show_Str_Mid(0,70,(char *)fensi,16,240, WHITE);
	get_fans();
}
void time_rate()
{
	EventBits_t uxBits;
	uxBits=xEventGroupGetBits(s_wifi_event_group);
	if(uxBits & WIFI_SMART)//如果开�??�??信配网程�??
	 	page_wifi_connet();
	uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT, true, false, portMAX_DELAY); 
        if(uxBits & CONNECTED_BIT) //配网成功
		{ 
		 	if(uxBits & WIFI_SMART)//如果开�??�??信配网程�??
			{
				lcdDrawFillRect(&DEV, 0, 0, 239, 134,BLACK);//清屏

			}
			char file[32];
			strcpy(file, "/spiffs/wifi.png");
			PNGTest(&DEV, file, 32, 24,0,0);
			Show_Str_Mid(0,70,(char *)Sgettime,16,240, WHITE);
			time_init();
        }
}
void weather_rate()
{
	Show_Str_Mid(0,70,(char *)gettianqi,16,240, WHITE);
	get_weather();
}
void show_fans(uint16_t *  data)
{
	

	lcd_draw_utf8_ram(0,4,"粉丝",40,WHITE,data,80,50);
	lcd_draw_utf8_ram(0,28,"关注",40,WHITE,data,80,50);

	uint8_t now_time[40];
	sprintf((char *)now_time,"%d",wp_fans.follower);
	lcd_RAMDrawString(fx16M, 50, 6,now_time, WHITE,data,80);

	sprintf((char *)now_time," %d",wp_fans.following);
	lcd_RAMDrawString(fx16M, 50, 30,now_time, WHITE,data,80);
	

}
void show_power()
{
	char file[30];
	strcpy(file, "/spiffs/power.png");
	for(uint8_t i=0;i<200;i++)
	{
		PNGTest(&DEV, file, 24, 24,i,0);
	}
	ESP_LOGI(TAG, "显示电量");
}
void page_on()
{
	lcdFillScreen(&DEV, BLACK);//清屏
	//show_power();
	showlogo();    
	jindu(0,WHITE);
	show_rate(wifi_rate,10,WHITE);
	show_rate(time_rate,30,WHITE);
	show_rate(fans_rate,50,WHITE);
	show_rate(weather_rate,80,WHITE);
	jindu(100,WHITE);
	page_evt_queue = xQueueCreate(5, sizeof(uint8_t));
	WAIT(50);
		
}
void write_time_ram(uint16_t x,uint16_t y,uint16_t color,uint16_t * data,int w,uint8_t flag)
{
	uint8_t now_time[40];
	struct tm timeinfo;
	get_time(&timeinfo);
	if(flag)
	sprintf((char *)now_time,"%02d:%02d",timeinfo.tm_hour,timeinfo.tm_min);
	else
	sprintf((char *)now_time,"%02d %02d",timeinfo.tm_hour,timeinfo.tm_min);
	
	lcd_RAMDrawString(fx32M, 0, 0, now_time, WHITE,data,w);

	sprintf((char *)now_time,"%02d/%02d",timeinfo.tm_mon+1,timeinfo.tm_mday);
	lcd_RAMDrawString(fx16M, 0, 32, now_time, WHITE,data,w);

	switch (timeinfo.tm_wday)
	{
	case 0:
		lcd_draw_utf8_ram(40,32,"周日",40,WHITE,data,82,48);
		break;
	case 1:
		lcd_draw_utf8_ram(40,32,"周一",40,WHITE,data,82,48);
		break;
	case 2:
		lcd_draw_utf8_ram(40,32,"周二",40,WHITE,data,82,48);
		break;
	case 3:
		lcd_draw_utf8_ram(40,32,"周三",40,WHITE,data,82,48);
		break;
	case 4:
		lcd_draw_utf8_ram(40,32,"周四",40,WHITE,data,82,48);
		break;
	case 5:
		lcd_draw_utf8_ram(40,32,"周五",40,WHITE,data,82,48);
		break;
	case 6:
		lcd_draw_utf8_ram(40,32,"周六",40,WHITE,data,82,48);
		break;

	default:
		lcd_draw_utf8_ram(40,32,"纳尼",40,WHITE,data,82,48);
		break;
		break;
	}
}
void draw_weather_Ram(uint8_t i,int x,int y,uint8_t * data,uint8_t w)
{
    char file[30];
    sprintf(file,"/spiffs/%s",weather[i].filename);
	PNG_SHOW(file, 48, 48,x,y,data,w);
}
void draw_wenshi(int x,int y,uint8_t * data,uint8_t w)
{
	char now_time[40];
	sprintf(now_time,"%d C",wp.temp);
	lcd_RAMDrawString(fx24M, x, y, (uint8_t *)now_time, WHITE,(uint16_t * )data,w);
	sprintf(now_time,"%d %%",wp.hus);

	lcd_RAMDrawString(fx24M, x, y+24, (uint8_t *)now_time, WHITE,(uint16_t * )data,w);
}

typedef struct _dev {
	char *str;
	//uint8_t *p;
	int16_t start_xy[2];
	int16_t end_xy[2];
	uint8_t w;
	uint8_t h;
} LCDDIV;

void page_test(void *pvParameter)
{
	CLEAN_LCD
	LCDDIV div[4]=
	 {{"/spiffs/2.png",{-80,83},{0,83},80,48},



	 };

}
void page_first(void *pvParameter)
{
	CLEAN_LCD
	uint8_t * b22n=calloc(2 * 82*48,sizeof(uint8_t));//22娘 80 0   80
	uint8_t * b23n=calloc(2 * 82*48,sizeof(uint8_t));//23娘 160 0  40
	uint8_t * time=calloc(2 * 82*48,sizeof(uint8_t));//时间 10 30  90
	uint8_t * weath=calloc(2 * 122*48,sizeof(uint8_t));//天气+温度 140 30   60
	uint8_t * bshu=calloc(2 * 80*50,sizeof(uint8_t));//b数 140 30   60
	/*加载到内存*/
	PNG_SHOW("/spiffs/2.png", 80, 48,1,0,b22n,82);
	PNG_SHOW("/spiffs/3.png", 80, 48,1,0,b23n,82);
	write_time_ram(1,0,WHITE,(uint16_t * )time,82,1);
	draw_weather_Ram(weather_data[0].code_day,1,0,weath,122);
	draw_wenshi(71,0,weath,122);
	/*开启动画*/
	show_fans((uint16_t * )bshu);
	for(int i=0;i<80;i++)
	{   
		lcd_fastDraw_xy(&DEV,i-80, 83,82,48,b22n);
		lcd_fastDraw_xy(&DEV,i-60, 30,82,48,time);
		if(i>=40)
		{
			lcd_fastDraw_xy(&DEV, 237-i, 83,82,48,b23n);
			lcd_fastDraw_xy(&DEV, 197-i, 30,122,48,weath);
			lcd_fastDraw_xy(&DEV,80, 165-i,80,50,bshu);
		}
	}
	uint8_t num=0,i=0;
	while(1)
	{
		i++;
		if(i%2)
		write_time_ram(0,0,WHITE,(uint16_t * )time,82,1);
		else
		write_time_ram(0,0,WHITE,(uint16_t * )time,82,0);
		if(i==14)
		{
			i=0;
			draw_wenshi(71,0,weath,122);
		}
		lcd_fastDraw_xy(&DEV,20, 30,82,48,time);
		lcd_fastDraw_xy(&DEV, 120, 30,122,48,weath);
		xQueueReceive( page_evt_queue, &num, 40 );//400ms
		if(num==MENU_RES)//退出
		{
				for(int j=0;j<80;j+=2)
				{   
					lcd_fastDraw_xy(&DEV,0-j, 83,82,48,b22n);
					lcd_fastDraw_xy(&DEV,20-j, 30,82,48,time);
					lcd_fastDraw_xy(&DEV, 160+j, 83,82,48,b23n);
					lcd_fastDraw_xy(&DEV, 120+j, 30,122,48,weath);
					lcd_fastDraw_xy(&DEV,80, 85+j,80,50,bshu);
					
				}
			CLEAN_LCD
			free(time);
			free(weath);
			free(bshu);
			free(b22n);
			free(b23n);
			num=MENU_OK;
			xQueueSend(page_evt_queue, &num, 10);//63
			ESP_LOGI(TAG,"页面一销毁");
			vTaskDelete(NULL);
		}

	}


}
void page_second(void *pvParameter)
{
	static TickType_t startTick, endTick, diffTick;
	startTick = xTaskGetTickCount();
	char str[50];
	uint8_t * weath=calloc(2 * 50*48,sizeof(uint8_t));//天气 20 30
	uint8_t * car=calloc(2 * 82*48,sizeof(uint8_t));//车 0 80
	uint8_t * text=calloc(2 *180*100,sizeof(uint8_t));//text 90 30
	/*加载到内存*/
	PNG_SHOW("/spiffs/car.png", 80, 48,1,0,car,82);
	draw_weather_Ram(weather_data[0].code_day,1,0,weath,50);

	lcd_draw_utf8_ram(0,0,"今日天气",150,WHITE,(uint16_t *)text,180,100);

	sprintf(str,"%s  %d-%d度",weather_data[0].text_day,weather_data[0].low,weather_data[0].high);
	lcd_draw_utf8_ram(0,20,str,150,WHITE,(uint16_t *)text,180,100);

	sprintf(str,"%s风 %d %.1fkm/h",weather_data[0].wind_direction,weather_data[0].wind_direction_degree,weather_data[0].wind_speed);
	lcd_draw_utf8_ram(1,40,str,150,WHITE,(uint16_t *)text,180,100);

	sprintf(str,"湿度 %d %%",weather_data[0].humidity);
	lcd_draw_utf8_ram(0,60,str,150,WHITE,(uint16_t *)text,180,100);

	sprintf(str,"降雨量 %.1f mm",weather_data[0].rainfall);
	lcd_draw_utf8_ram(0,80,str,150,WHITE,(uint16_t *)text,180,100);

	for(int i=0;i<80;i++)
	{   

		lcd_fastDraw_xy(&DEV,i-80, 83,82,48,car);
		lcd_fastDraw_xy(&DEV,170-i, 30,180,100,text);
		if(i>40)
		lcd_fastDraw_xy(&DEV,i-60, 30,50,48,weath);
	}
	endTick = xTaskGetTickCount();
    diffTick = endTick - startTick;
	ESP_LOGI("TIME","用时 %d ms",diffTick*10);
	uint8_t num=1;
	while(1)
	{
		xQueueReceive( page_evt_queue, &num, portMAX_DELAY );
		if(num==MENU_RES)//退出
		{
			for(int i=0;i<80;i+=2)
			{   
				lcd_fastDraw_xy(&DEV,-i, 83,82,48,car);
				lcd_fastDraw_xy(&DEV,90+i, 30,180,100,text);
				if(i>40)
					lcd_fastDraw_xy(&DEV,60-i, 30,50,48,weath);
				
			}
			CLEAN_LCD
			free(car);
			free(weath);
			free(text);
			num=MENU_OK;
			xQueueSend(page_evt_queue, &num, 10);//63
			vTaskDelete(NULL);
		}

	}
	
}
void page_three(void *pvParameter)
{
	uint16_t * data=(uint16_t*)malloc(2 * 102*240);
	memset(data,0,2 * 102*240);
	show_weather(data);

	lcd_fastDraw_xy(&DEV, 0, 30,240,102,(uint8_t*)data);
	free(data);
	uint8_t num=2;
	while(1)
	{
		xQueueReceive( page_evt_queue, &num,portMAX_DELAY);
		if(num==MENU_RES)//退出
		{
			CLEAN_LCD
			num=MENU_OK;
			xQueueSend(page_evt_queue, &num, NULL);//63
			vTaskDelete(NULL);
		}

	}
}
void page_four(void *pvParameter)
{
	CLEAN_LCD
	char info[50];
	uint8_t num=2;
	lcd_show_utf8(0,30,"web管理",240,WHITE);//天气温度

	sprintf(info,"ip:%s",ip_adder);

	lcd_show_utf8(0,50,info,240,WHITE);//天气温度

	sprintf(info,"%s/set.html",ip_adder);
	lcd_show_utf8(0,70,info,240,WHITE);//天气温度

	sprintf(info,"%s/main.html",ip_adder);
	lcd_show_utf8(0,90,info,240,WHITE);//天气温度
	while(1)
	{
		xQueueReceive(page_evt_queue, &num, portMAX_DELAY );
		if(num==MENU_RES)//退出
		{
			CLEAN_LCD
			num=MENU_OK;
			xQueueSend(page_evt_queue, &num, 10);//63
			vTaskDelete(NULL);
		}

	}
}
void page_time(void *pvParameter)
{

}